<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Jobs\ProcessCompany;
use App\News;
use App\Label;
use App\Company;
use App\Keyword;
use App\Industry;

class DataController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    public function processC()
    {
        set_time_limit (0);
        $fileName = 'has_news_company_name.txt';

        $filePath = resource_path('data/' . $fileName);

        if (file_exists($filePath)) {
            $file = file($filePath);

            for ($i=0; $i < count($file); $i++) { 
                $company = new Company;
                $company->name = $file[$i];
                $company->save();
            }
        } else {
            echo "no";
        }

        echo "Finish process company" . PHP_EOL;

        return ['code' => 0];
    }

    public function processCN()
    {
        set_time_limit (0);
        
        $fileName = 'has_news_company.json';

        $filePath = resource_path('data/' . $fileName);

        if (file_exists($filePath)) {
            $file = file($filePath);

            for ($i=0; $i < count($file); $i++) { 
                $line = json_decode($file[$i], true);

                $news = new News;
                $news->save();
                if (isset($line['news_title'])) {
                    $news->title = $line['news_title'];
                }

                if (isset($line['news_content'])) {
                    $news->content = $line['news_content'];
                }

                if (isset($line['news_label'])) {
                    $news->label = $this->getLabel($line['news_label']) + 1;

                    $label = Label::where('id', $news->label)
                        ->first();

                    $labelRelative = $label->relative;

                    if (!isset($labelRelative)) {
                        $labelRelative = [];
                    } else {
                        $labelRelative = json_decode($labelRelative, true);
                    }

                    array_push($labelRelative, [
                        'type' => 'news',
                        'value' => $news->id,
                        'message' => $news->title
                    ]);

                    $label->relative = json_encode($labelRelative, true);
                    $label->save();
                }

                if (isset($line['company'])) {
                    $company = Company::where('name', 'like', '%' . $line['company'] . '%')
                        ->first();

                    $companyRelative = $company->relative;

                    if (!isset($companyRelative)) {
                        $companyRelative = [];
                    } else {
                        $companyRelative = json_decode($companyRelative, true);
                    }

                    array_push($companyRelative, [
                        'type' => 'news',
                        'value' => $news->id,
                        'message' => $news->title
                    ]);

                    $company->relative = json_encode($companyRelative, true);
                    $company->save();

                    $news->company = $company->id;
                }

                if (isset($line['people'])) {
                    $people = array_filter($line['people'], function($value) {

                        $len = mb_strlen(trim($value), 'utf-8');
                        if ($len > 1 && $len <= 3) {
                            return true;
                        }
                        return false;
                    });
                    $news->people = implode(",", $people);
                }

                $news->save();
            }
        }
    }

    public function processCIK()
    {
        set_time_limit (0);
        
        $fileName = 'company_industry_keywords.json';

        $filePath = resource_path('data/' . $fileName);

        if (file_exists($filePath)) {
            $file = file($filePath);

            for ($i=0; $i < count($file); $i++) { 
                $line = json_decode($file[$i], true);

                $keywords = $line['keywords'];
                $company = Company::where('name', 'like', '%' . $line['company'] . '%')
                    ->first();
                $industry = Industry::where('name', 'like', '%' . $line['industry'] . '%')
                    ->first();

                if (empty($company)) {
                    continue;
                }

                if (empty($industry)) {
                    $industry = new Industry;
                    $industry->name = $line['industry'];
                    $industry->save();
                }

                if (!isset($industry->relative)) {
                    $industryRelative = [];
                } else {
                    $industryRelative = json_decode($industry->relative, true);
                }

                if (!isset($company->relative)) {
                    $companyRelative = [];
                } else {
                    $companyRelative = json_decode($company->relative, true);
                }

                $industryId =  $industry->id;

                foreach ($keywords as $key) {
                    $keyword = Keyword::where('name', 'like', '%' . $key . '%')
                        ->first();

                    if (empty($keyword)) {
                        $keyword = new Keyword;
                        $keyword->name = $key;
                        $keyword->save();
                    }
 
                    if (!isset($keyword->relative)) {
                        $keywordRelative = [];
                    } else {
                        $keywordRelative = json_decode($keyword->relative, true);
                    }

                    array_push($keywordRelative, [
                        'type' => 'company',
                        'value' => $company->id,
                        'message' => $line['company']
                    ]);

                    array_push($keywordRelative, [
                        'type' => 'industry',
                        'value' => $industry->id,
                        'message' => $line['industry']
                    ]);

                    array_push($companyRelative, [
                        'type' => 'keyword',
                        'value' => $keyword->id,
                        'message' => $key
                    ]);

                    array_push($industryRelative, [
                        'type' => 'keyword',
                        'value' => $keyword->id,
                        'message' => $key
                    ]);

                    $keyword->relative = json_encode($keywordRelative, true);
                    $keyword->save();
                }

                array_push($industryRelative, [
                    'type' => 'company',
                    'value' => $company->id,
                    'message' => $line['company']
                ]);

                array_push($companyRelative, [
                    'type' => 'industry',
                    'value' => $industry->id,
                    'message' => $line['industry']
                ]);

                $industry->relative = json_encode($industryRelative, true);
                $industry->save();

                $company->relative = json_encode($companyRelative, true);
                $company->save();

                echo " " . $i . " ";
            }
        }
    }

    private function getLabel($labelName) {
        if ($labelName == "产品") {
            return 0;
        } else if ($labelName == "人事") {
            return 1;
        } else if ($labelName == "政策") {
            return 2;
        } else if ($labelName == "数据") {
            return 3;
        } else if ($labelName == "访谈") {
            return 4;
        } else if ($labelName == "资本") {
            return 5;
        }
    }

    public function search(Request $request) 
    {
        $query = trim($request->input('query'));

        // 优先搜索label
        $label = Label::where('name', 'like', '%' . $query . '%')
            ->first();

        if (! empty($label)) {
            return json_decode($label->relative, true);
        }

        // 然后搜索公司
        $company = Company::where('name', 'like', '%' . $query . '%')
            ->first();

        if (! empty($company)) {
            return json_decode($company->relative, true);
        }

        // 然后搜索行业
        $industry = Industry::where('name', 'like', '%' . $query . '%')
            ->first();

        if (! empty($industry)) {
            return json_decode($industry->relative, true);
        }

        // 最后搜索关键词
        $keyword = Keyword::where('name', 'like', '%' . $query . '%')
            ->first();

        if (! empty($keyword)) {
            return json_decode($keyword->relative, true);
        }

        return [];
    }

    public function news(Request $request)
    {
        $id = $request->input('id');

        $news = News::where('id', $id)
            ->first()
            ->toArray();

        return $news;
    }
}
